package com.moyq5.mvc.maker.test.bean.entity;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.PrePersist;
import javax.persistence.Table;

import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.GenericGenerator;

import com.moyq5.mvc.maker.test.bean.embed.Notify;
import com.moyq5.mvc.maker.test.bean.enums.CmdStatus;
import com.moyq5.mvc.maker.test.bean.enums.SysNotifyStatus;
import com.moyq5.mvc.maker.test.bean.enums.SysOperatorType;

/**
 * 指令记录
 * @author Moyq5
 * @date 2018年4月14日
 */
@Entity
@Table(name = "iot_cmd")
@DynamicUpdate
public class IotCmd implements java.io.Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * 指令ID
	 */
	@Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "native")@GenericGenerator(name = "native", strategy = "native")
    @Column(name = "cmd_id")
	private Long cmdId;
	/**
     * 指令序号（系统唯一）
     */
    @Column(name = "cmd_no", nullable = false, length = 32)
    private String cmdNo;
    /**
	 * 操作人类型
	 */
    @Enumerated(EnumType.ORDINAL)
    @Column(name = "operator_type", nullable = false, columnDefinition="int(1) default '0'")
	private SysOperatorType operatorType;
    /**
	 * 操作人（消息者）
	 */
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "user_id")
	private User operatorUser;
	/**
	 * 操作人（管理员）
	 */
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "admin_id")
	private SysAdmin operatorAdmin;
    /**
	 * 指令类型
	 */
    @Column(name = "cmd", nullable = false, columnDefinition="int(1)")
	private Integer cmd;
    /**
	 * 指令名称
	 */
	@Column(name = "name", length = 30)
	private String name;
	/**
	 * 指令内容
	 */
	@Column(name = "content", length = 300)
	private String content;
	/**
	 * 目标设备（为空时表示指令广播）
	 */
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "device_id")
	private IotDevice device;
	/**
     * 信息序列号
     */
    @Column(name = "series", nullable = false, precision = 8)
    private Integer series;
    /**
	 * 发送/响应状态
	 */
    @Enumerated(EnumType.ORDINAL)
    @Column(name = "status", nullable = false, columnDefinition="int(1)")
	private CmdStatus status;
    /**
     * 发送次数
     */
    @Column(name = "send_count", nullable = false, precision = 8)
    private Integer sendCount;
    /**
	 * （最后一次）发送时间
	 */
	@Column(name = "send_time", columnDefinition="datetime")
	private Date sendTime;
	/**
	 * 响应内容
	 */
	@Column(name = "back_content", length = 300)
	private String backContent;
	/**
	 * 响应时间
	 */
	@Column(name = "back_time", columnDefinition="datetime")
	private Date backTime;
	/**
	 * 添加时间
	 */
	@Column(name = "add_time", nullable = false, columnDefinition="datetime")
	private Date addTime;
	
	@Embedded
	private Notify notify;
	
	@PrePersist
	public void prePersist() {
		if (null == addTime) {
			addTime = new Date();
		}
		if (null == notify) {
			notify = new Notify();
		}
		if (null == notify.getNotifyStatus()) {
			notify.setNotifyStatus(SysNotifyStatus.NEW);
		}
		if (null == notify.getNotifyCount()) {
			notify.setNotifyCount(0);
		}
		if (null == notify.getNotifyRemain()) {
			notify.setNotifyRemain(10);
		}
		if (null == notify.getNotifyTime()) {
			notify.setNotifyTime(new Date());
		}
	}
	public Long getCmdId() {
		return cmdId;
	}
	public void setCmdId(Long cmdId) {
		this.cmdId = cmdId;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public IotDevice getDevice() {
		return device;
	}
	public void setDevice(IotDevice device) {
		this.device = device;
	}
	public Integer getSeries() {
		return series;
	}
	public void setSeries(Integer series) {
		this.series = series;
	}
	public CmdStatus getStatus() {
		return status;
	}
	public void setStatus(CmdStatus status) {
		this.status = status;
	}
	public Integer getSendCount() {
		return sendCount;
	}
	public void setSendCount(Integer sendCount) {
		this.sendCount = sendCount;
	}
	public Date getSendTime() {
		return sendTime;
	}
	public void setSendTime(Date sendTime) {
		this.sendTime = sendTime;
	}
	public String getBackContent() {
		return backContent;
	}
	public void setBackContent(String backContent) {
		this.backContent = backContent;
	}
	public Date getBackTime() {
		return backTime;
	}
	public void setBackTime(Date backTime) {
		this.backTime = backTime;
	}
	public Date getAddTime() {
		return addTime;
	}
	public void setAddTime(Date addTime) {
		this.addTime = addTime;
	}
	public String getCmdNo() {
		return cmdNo;
	}
	public void setCmdNo(String cmdNo) {
		this.cmdNo = cmdNo;
	}
	public SysOperatorType getOperatorType() {
		return operatorType;
	}
	public void setOperatorType(SysOperatorType operatorType) {
		this.operatorType = operatorType;
	}
	public User getOperatorUser() {
		return operatorUser;
	}
	public void setOperatorUser(User operatorUser) {
		this.operatorUser = operatorUser;
	}
	public SysAdmin getOperatorAdmin() {
		return operatorAdmin;
	}
	public void setOperatorAdmin(SysAdmin operatorAdmin) {
		this.operatorAdmin = operatorAdmin;
	}
	public Integer getCmd() {
		return cmd;
	}
	public void setCmd(Integer cmd) {
		this.cmd = cmd;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Notify getNotify() {
		return notify;
	}
	public void setNotify(Notify notify) {
		this.notify = notify;
	}
	
}
